package com.atxiaodei.springbootkabudazkapi.zookeepercrud;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.data.Stat;

import java.util.List;

/**
 * @ClassName GetZookeeperDemo
 * @Deacription TODO
 * @Author 卡布达
 * @Date 2022/1/16 22:13
 * @Version 1.0
 **/
public class GetZookeeperDemo {


    public void get() throws Exception {

        CuratorFramework client= CuratorFrameworkFactory
                .builder()
                .connectionTimeoutMs(50000)
                .connectString("192.168.0.108:2181") //读写分离(zookeeper-server)
                // baseSleepTimeMs*Math.max(1,random.nextInt(1<<(maxRetries+1))
                /**
                 * RetryNTimes 指定最大重试次数
                 * RetryOneTimes
                 * RetryUntilElapsed 一直重试，直到达到规定时间
                 */
                .retryPolicy(new ExponentialBackoffRetry(1000,3))
                .sessionTimeoutMs(15000)
                // 名称空间 每次操作zk /kabuda
                .namespace("kabuda")
                .build();
        client.start(); //启动

        // 获取节点数据
        byte[] bytes = client.getData().forPath("/app1");
        System.out.println(new String(bytes));
        // ls
        List<String> list = client.getChildren().forPath("/");
        System.out.println(list);
        // ls2 ls -s
        Stat stat = new Stat();
        byte[] bytes1 = client.getData().storingStatIn(stat).forPath("/app2");
        System.out.println(new String(bytes1));
        System.out.println(stat);
    }

    public static void main(String[] args) throws Exception {
        GetZookeeperDemo getZookeeperDemo=new GetZookeeperDemo();
        getZookeeperDemo.get();
    }
}
